package cn.edu.bzu.group4.stockplus.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.edu.bzu.group4.stockplus.common.base.controller.BaseController;
import cn.edu.bzu.group4.stockplus.po.User;
import cn.edu.bzu.group4.stockplus.service.UserServiceImpl;
import cn.edu.bzu.group4.stockplus.validator.LoginCommand;
import cn.edu.bzu.group4.stockplus.validator.LoginValidator;

@Controller
@RequestMapping(value="/")
public class UserController { 
  
 
	@Autowired
	private UserServiceImpl userserivceimpl = null;
	
	private ModelAndView mv =null;
	private User user = null;
	
	private String loginFlag = "0";
	
	private Map<String,String> map = new HashMap<String, String>();
	
	
	public void setMap(){
			map.put("loginFlag", loginFlag);
	}
    @RequestMapping(value="/login",method= RequestMethod.GET)
    public void showLoginForm(String tel,String password,HttpSession session) {
        login(tel,password,session);
    }
	
	@RequestMapping(value="/login",method={RequestMethod.POST})
	public ModelAndView login(String tel,String password,HttpSession session){
		System.out.println("用户提交的数据:"+tel+password);
//		if(userserivceimpl.selectUser(tel, password)){
		if("15762182153".equals(tel) && "E10ADC3949BA59ABBE56E057F20F883E".equals(password)){
			System.out.println("login success");
			session.setAttribute("user", tel);
			 return new ModelAndView("index");
		}
		
		
		else{
			System.out.println("login error");
			return new ModelAndView("404");
		}
	}
	
	//退出操作
	@RequestMapping("logout")
	public ModelAndView logout(HttpSession session){
		session.removeAttribute("user");
		return new ModelAndView("index");
	}
	
	
	@RequestMapping(value="regist")
	public ModelAndView regist(String username,String usertel,String usereml,String pwd,String sex,String surf,String takepic){
		
		if(username==null){
			return new ModelAndView("regist");
		}
		
		if(surf==null){
			surf = "";
		}
		
		if(takepic==null){
			takepic = "";
		}
		
		
		System.out.println("USER INFO: "+usereml+username+usertel+pwd+sex+surf+takepic);
		
		user = new User(); 
		user.setUserName(username);
		user.setUserTel(usertel);
		user.setUserPassword(pwd);
		user.setUserEmail(usereml);
		user.setUserSex(sex);
		user.setUserHobby(surf+takepic);
		String now =Long.toString(
				new Date().getTime()
				); 
		user.setRecordDate(now);
		
		short openAccountOrNot = 0;
		user.setUserAccount(openAccountOrNot);
		
//		if(userservicei.userRegist(user)){
//			System.out.println("注册 success");
//		}else{
//			System.out.println("注册 error");
//		}
		mv = new ModelAndView("usercenter");
		mv.addObject("title", "注册成功");
		
		return mv;
	}
	
	
	@RequestMapping(value="/user",method={RequestMethod.POST})
	public ModelAndView usercenter(){
		//首先权限验证，验证是否登录
		mv = new ModelAndView("index");
		mv.addObject("status","0");
		return mv;
	}
	
	@RequestMapping(value="/user",method={RequestMethod.GET})
	public void usr(){
		usercenter();
	}
	
} 
